package PTA_JAVA.JavaA.Javademo29;


import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m=0;
        int n=0;
        ArrayListGeneralStack stack= new ArrayListGeneralStack();
        while(sc.hasNext()){
            String s=sc.next();
            if(s.equals("quit")){
                break;
            }else if(s.equals("Integer")){
                m = sc.nextInt();
                n = sc.nextInt();
                System.out.println("Integer Test");
                stack= new ArrayListGeneralStack();
                for (int i = 0; i < m; i++) {
                    stack.push(sc.nextInt());
                    System.out.println("push:"+stack.peek());
                }
                for (int i = 0; i < n; i++) {
                    System.out.println("pop:"+stack.pop());
                }
                System.out.println(stack.toString());
                int sum=0;
                while(!stack.empty()) {
                    sum += (int) stack.pop();
                }
                System.out.println("sum="+sum);
                System.out.println(stack.getClass().getInterfaces()[0]);
            }else if(s.equals("Double")){
                m = sc.nextInt();
                n = sc.nextInt();
                System.out.println("Double Test");
                stack= new ArrayListGeneralStack();
                for (int i = 0; i < m; i++) {
                    stack.push(sc.nextDouble());
                    System.out.println("push:"+stack.peek());
                }
                for (int i = 0; i < n; i++) {
                    System.out.println("pop:"+stack.pop());
                }
                System.out.println(stack.toString());
                double sum=0;
                while(!stack.empty()) {
                    sum += (double) stack.pop();
                }
                System.out.println("sum="+sum);
                System.out.println(stack.getClass().getInterfaces()[0]);
            }else if(s.equals("Car")){
                m = sc.nextInt();
                n = sc.nextInt();
                System.out.println("Car Test");
                stack= new ArrayListGeneralStack();
                for (int i = 0; i < m; i++) {
                    Car c=new Car();
                    c.setId(sc.nextInt());
                    c.setName(sc.next());
                    stack.push(c);
                    System.out.println("push:"+stack.peek());
                }
                for (int i = 0; i < n; i++) {
                    System.out.println("pop:"+stack.pop());
                }
                System.out.println(stack.toString());
                while(!stack.empty()) {
                    Car c = new Car();
                    c = (Car) stack.pop();
                    System.out.println(c.getName());
                }
                System.out.println(stack.getClass().getInterfaces()[0]);
            }
        }
    }
}
interface GeneralStack {
    Object push(Object item);            //如item为null，则不入栈直接返回null。
    Object pop();                 //出栈，如为栈为空，则返回null。
    Object peek();                //获得栈顶元素，如为空，则返回null.
    public boolean empty();//如为空返回true
    public int size();     //返回栈中元素数量
}
class ArrayListGeneralStack implements GeneralStack{
    ArrayList list=new ArrayList<>();
    @Override
    public Object push(Object item) {
        if(item==null){
            return null;
        }
        return list.add(item);
    }

    @Override
    public Object pop() {
        if(list.size()==0){
            return null;
        }
        Object a=list.get(list.size()-1);
        list.remove(list.size()-1);
        return a;
    }

    @Override
    public Object peek() {
        if(list.size()==0){
            return null;
        }else{
            return list.get(list.size()-1);
        }
    }

    @Override
    public boolean empty() {
        return list.isEmpty();
    }

    @Override
    public int size() {
        return list.size();
    }

    @Override
    public String toString() {
        return list.toString();
    }
}
class Car{
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Car [" +
                "id=" + id +
                ", name=" + name +
                ']';
    }
}